package com.gengzp.rpc.model.common.resp;

import java.io.Serial;
import java.io.Serializable;

import static com.gengzp.rpc.constants.RpcRequestCodeConstants.RPC_REQUEST_FAIL;
import static com.gengzp.rpc.constants.RpcRequestCodeConstants.RPC_REQUEST_SUCCESS;

/**
 * @ClassName RpcResponse
 * @Description 远程调用响应结果
 * @Author gengzp
 * @Date 2025/8/20 15:53
 */
public class RpcResponse<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -9089006407531395353L;

    /**
     * 返回码
     */
    private int code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 远程调用返回业务数据
     */
    private T data;

    /**
     * 启用远程事务标识
     */
    private Boolean enableRemoteTransactionFlag;

    private RpcResponse() {
    }

    public static <T> RpcResponse<T> success(T data, Boolean enableRemoteTransactionFlag) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setCode(RPC_REQUEST_SUCCESS);
        response.setData(data);
        response.setEnableRemoteTransactionFlag(enableRemoteTransactionFlag);
        return response;
    }

    public static <T> RpcResponse<T> fail(String message, Boolean enableRemoteTransactionFlag) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setCode(RPC_REQUEST_FAIL);
        response.setMessage(message);
        response.setEnableRemoteTransactionFlag(enableRemoteTransactionFlag);
        return response;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean getEnableRemoteTransactionFlag() {
        return enableRemoteTransactionFlag;
    }

    public void setEnableRemoteTransactionFlag(Boolean enableRemoteTransactionFlag) {
        this.enableRemoteTransactionFlag = enableRemoteTransactionFlag;
    }

}
